package org.networking.http;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	public static void main(String[] args) throws Exception {
		ServerSocket server = new ServerSocket(8010);
		Socket socket = server.accept();
		
		System.out.println("服务器启动，接收请求");
		
		//读取输入流（请求）
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		char[] buffer = new char[1024];
		int len = br.read(buffer);
		StringBuffer reqStr = new StringBuffer();
		for(int i=0;i<len;i++){
			reqStr.append(buffer[i]);
		}
		System.out.println(reqStr.toString());
		
		//写入输出流（响应）
		PrintWriter pw = new PrintWriter(socket.getOutputStream());
		pw.println("<h1>hello world!</h1>");
		pw.flush();
		socket.close();
	}
}
